package com.study.app.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.app.domain.Faq;
import com.study.app.domain.bo.FaqBo;
import com.study.app.domain.vo.FaqVo;
import com.study.app.mapper.FaqMapper;
import com.study.app.service.IFaqService;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * FAO InfoService业务层处理
 *
 * @author study
 * @date 2023-10-10
 */
@RequiredArgsConstructor
@Service
public class FaqServiceImpl implements IFaqService {

    private final FaqMapper baseMapper;

    /**
     * 查询FAO Info
     */
    @Override
    public FaqVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询FAO Info列表
     */
    @Override
    public TableDataInfo<FaqVo> queryPageList(FaqBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Faq> lqw = buildQueryWrapper(bo);
        Page<FaqVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询FAO Info列表
     */
    @Override
    public List<FaqVo> queryList(FaqBo bo) {
        LambdaQueryWrapper<Faq> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Faq> buildQueryWrapper(FaqBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Faq> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLangType()), Faq::getLangType, bo.getLangType());
        lqw.eq(bo.getFaqType() != null, Faq::getFaqType, bo.getFaqType());
        lqw.eq(StringUtils.isNotBlank(bo.getTitleInitials()), Faq::getTitleInitials, bo.getTitleInitials());
        lqw.eq(bo.getSort() != null, Faq::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), Faq::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getSubtitle()), Faq::getSubtitle, bo.getSubtitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContext()), Faq::getContext, bo.getContext());
        lqw.eq(bo.getIsPath() != null, Faq::getIsPath, bo.getIsPath());
        lqw.eq(StringUtils.isNotBlank(bo.getToPath()), Faq::getToPath, bo.getToPath());
        lqw.eq(StringUtils.isNotBlank(bo.getImgPath()), Faq::getImgPath, bo.getImgPath());
        return lqw;
    }

    /**
     * 新增FAO Info
     */
    @Override
    public Boolean insertByBo(FaqBo bo) {
        Faq add = BeanUtil.toBean(bo, Faq.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改FAO Info
     */
    @Override
    public Boolean updateByBo(FaqBo bo) {
        Faq update = BeanUtil.toBean(bo, Faq.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Faq entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除FAO Info
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
